package manager;

/**
 * @author huangjm.2012, mohameds.2012, jaslin.wee.2012, huiwensong.2012, sheryl.tay.2012
 * 
 */

import java.sql.SQLException;
import java.util.ArrayList;

import manager.database.DatabaseMgr;
import manager.entitycreator.StudentCreator;
import util.ValidationUtil;
import entity.Bid;
import entity.Student;

public class StudentMgr {

	private DatabaseMgr dbMgr = new DatabaseMgr();
	private StudentCreator studentCreator = new StudentCreator();

	public boolean addStudent(Student student) {
		String sql = "insert into student "
				+ " (student_userId, student_password, student_name, student_school, student_edollar) "
				+ " values (?,?,?,?,?)";
		int numRowsAffected = dbMgr.executeUpdate(sql, student.getUserId(),
				student.getPassword(), student.getName(), student.getSchool(),
				student.getEdollar());
		return numRowsAffected > 0;
	}

	public int[] addMultipleStudents(ArrayList<Student> students) {
		String sql = "insert into student "
				+ " (student_userId, student_password, student_name, student_school, student_edollar) "
				+ " values (?,?,?,?,?)";

		int[] numRowsAffected = { 0 };
		try {
			dbMgr.open();

			dbMgr.setStatement(sql);
			for (Student eachStudent : students) {
				dbMgr.addBatch(eachStudent.getUserId(),
						eachStudent.getPassword(), eachStudent.getName(),
						eachStudent.getSchool(), eachStudent.getEdollar());
			}
			numRowsAffected = dbMgr.executeBatch();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			dbMgr.close();
		}
		return numRowsAffected;
	}

	public void reset() {
		dbMgr.executeUpdate("delete from student");
	}

	public ArrayList<Student> retrieveAllStudents() {
		String sql = "select * from student order by student_userId";
		return dbMgr.retrieveMultipleEntitiesForQuery(studentCreator, sql);
	}

	public Student retrieveStudentForUserId(String userId) {
		String sql = "select * from student where student_userId=? order by student_userId";
		Student student = dbMgr.retrieveEntityForQuery(studentCreator, sql,
				userId);

		ValidationUtil.checkForElement(student);

		return student;
	}

	public void updateEdollarForStudent(double newAmount, String userId) {
		String sql = "update student set student_edollar=? where student_userId=?";
		dbMgr.executeUpdate(sql, newAmount, userId);
	}

	public void refundEdollarForStudent(double refundAmount, String userId) {
		String sql = "update student set student_edollar=student_edollar+? where student_userId=?";
		dbMgr.executeUpdate(sql, refundAmount, userId);
	}
	
	public int[] refundEdollars(ArrayList<Bid> bids) {
		String sql = "update student set student_edollar=student_edollar+? where student_userId=?";

		int[] numRowsAffected = { 0 };
		try {
			dbMgr.open();

			dbMgr.setStatement(sql);

			for (Bid eachBid : bids) {
				double refundAmount = eachBid.getBidAmount();
				String userId = eachBid.getStudent().getUserId();
				dbMgr.addBatch(refundAmount, userId);
			}

			numRowsAffected = dbMgr.executeBatch();
			dbMgr.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return numRowsAffected;
	}

}
